package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.sdf
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.data.model.BorrowNotifyResModel
import kotlinx.android.synthetic.main.frag_borrow_apply_result_notify.*

/**
 * the notify result of the borrow apply
 */
class BorrowApplyResultNotifyFragment : BaseFragment() {
    private var taskId = ""
    private var msgType = ""

    companion object {
        fun newInstance(taskId: String, msgType: String): BorrowApplyResultNotifyFragment {
            val target = BorrowApplyResultNotifyFragment()
            val args = Bundle()
            args.putString("taskId", taskId)
            args.putString("msgType", msgType)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_borrow_apply_result_notify
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        arguments?.apply {
            taskId = getString("taskId")
            msgType = getString("msgType")
        }
        iv_back.setOnClickListener { pop() }
    }

    @SuppressLint("SetTextI18n")
    private fun updateView(data: BorrowNotifyResModel) {
        val result = data.resultMaps[0]
        var des: String = ""
        when (result.state) {
            "1" -> {
                des = "您借阅的《${result.name}》正在审核中"
                iv_seal.gone(true)
                tv_hint.gone(true)
            }
            "2" -> {
                des = "您借阅的《${result.name}》已于${sdf().format(result.endTime)}通过审核，借阅结束时间：${sdf().format(result.returnTime)}"
                iv_seal.setImageResource(R.drawable.ic_borrow_pass)
                tv_hint.gone(false)
            }
            "3" -> {
                des = "您借阅的《${result.name}》已被拒绝，详情咨询您的上级同事"
                iv_seal.setImageResource(R.drawable.ic_borrow_reject)
                tv_hint.gone(true)
            }
            else -> {
                des = "您借阅的《${result.name}》正在等待审核处理"
                iv_seal.gone(true)
                tv_hint.gone(true)
            }

        }
        tv_borrow_state.text = "借阅申请${result.taskstateFlag}"
        tv_borrow_content.text = des
        tv_handle_man.text = "处理人：${result.approverUserName}"
        tv_state.text = result.taskstateFlag
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        //fetchBorrowResult()
    }

    @SuppressLint("CheckResult")
    private fun fetchBorrowResult() {
        val params = mutableMapOf(
                Pair(Const.qr_taskId, taskId),
                Pair(Const.qr_messageType, msgType)
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_borrow_notfiy_detail}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, BorrowNotifyResModel::class.java)?.apply {
                                if (code == "200")
                                    updateView(this)
                                else
                                    error(msg)

                            }
                        },
                        {
                            hideLoader()
                            it.printStackTrace()
                            report(it)
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading()
                        }
                )
    }
}